package com.nisco.dms.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Program;
import com.nisco.dms.service.ProgramService;
import com.nisco.dms.util.ControllerUtil;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;

@Controller
@RequestMapping(value="/program")
public class ProgramController
{
	private static final String LIST="program/programList";
	private static final String PRECREATE="program/programCreate";
	private static final String PREUPDATE="program/programUpdate";
	private static String  moduleListNavId = "";
	@Autowired
	private ProgramService programService;
	@RequestMapping(value="/list")
	public String list(Page page,Model model,String keywords,HttpServletRequest request)
	{
		moduleListNavId = ControllerUtil.navId(moduleListNavId, (String)request.getParameter("moduleListNavId"));
		if(!"".equals(keywords)&&keywords!=null)
			model.addAttribute("programList", programService.findProgramByKeywords(page, keywords));
		else
			model.addAttribute("programList", programService.findAll(page));
		model.addAttribute("page", page);
		model.addAttribute("keywords", keywords);
		return LIST;
	}
	@RequestMapping(value="/preCreate")
	public  String preCreate()
	{
		return PRECREATE;
	}
	@RequestMapping(value="/create")
	public@ResponseBody String create(Program program)
	{
		programService.save(program);
		AjaxObject ajaxObject=new AjaxObject();
		ajaxObject.setMessage("程序添加成功!");
		ajaxObject.setNavTabId(moduleListNavId);
		return ajaxObject.toString();
		
	}
	@RequestMapping(value="/preUpdate/{id}")
	public String preUpdate(@PathVariable(value="id") String id,Map<String,Program> map)
	{
		map.put("program", programService.get(id));
		return PREUPDATE;
	}
	
	@RequestMapping(value="/update")
	public @ResponseBody String update(Program program)
	{
		programService.update(program);
		AjaxObject ajaxObject=new AjaxObject();
		ajaxObject.setMessage("程序修改成功!");
		ajaxObject.setNavTabId(moduleListNavId);
		return ajaxObject.toString();
	}
	
	@RequestMapping(value="/delete/{id}")
	public @ResponseBody String delete(@PathVariable(value="id") String id)
	{
		programService.delete(id);
		AjaxObject ajaxObject=new AjaxObject();
		ajaxObject.setMessage("程序删除成功!");
		ajaxObject.setNavTabId(moduleListNavId);
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}

}
